package com.financial.business.yspay.service.impl;

import com.financial.business.yspay.constants.FinancialPayYsEnumConstants;
import com.financial.business.yspay.constants.FinancialPaymentYsEnumConstants;
import com.financial.business.yspay.constants.FinancialSplitYsEnumConstants;
import com.financial.business.yspay.constants.FinancialWithdrawalYsEnumConstants;
import com.financial.business.yspay.constants.asyn.FinancialPayAsyncEnumConstants;
import com.financial.business.yspay.constants.asyn.FinancialPaymentAsyncEnumConstants;
import com.financial.business.yspay.constants.asyn.FinancialSplitAsyncEnumConstants;
import com.financial.business.yspay.constants.asyn.FinancialWithdrawalAsyncEnumConstants;
import com.financial.business.yspay.domain.asyn.FinancialPayAsyncDO;
import com.financial.business.yspay.domain.asyn.FinancialPaymentAsyncDO;
import com.financial.business.yspay.domain.asyn.FinancialSplitAsyncDO;
import com.financial.business.yspay.domain.asyn.FinancialWithdrawalAsyncDO;
import com.financial.business.yspay.service.*;
import com.financial.business.yspay.service.asyn.FinancialPayAsyncService;
import com.financial.business.yspay.service.asyn.FinancialPaymentAsyncService;
import com.financial.business.yspay.service.asyn.FinancialSplitAsyncService;
import com.financial.business.yspay.service.asyn.FinancialWithdrawalAsyncService;
import com.financial.business.yspay.utils.HttpClientUtils;
import com.financial.business.yspay.utils.JsonUtils;
import com.financial.business.yspay.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //银盛微信支付异步回执
 * @Date 2019/11/14 11:11
 */
@Service
public class FinancialYsCallBackServiceImpl implements FinancialYsCallBackService {
    public static final Logger logger = LoggerFactory.getLogger(FinancialYsCallBackServiceImpl.class);
    @Autowired
    private FinancialPayAsyncService financialPayAsyncService;

    @Autowired
    private FinancialPayYsService financialPayYsService;

    @Autowired
    private FinancialPaymentAsyncService financialPaymentAsyncService;

    @Autowired
    private FinancialPaymentYsService financialPaymentYsService;

    @Autowired
    private FinancialWithdrawalAsyncService financialWithdrawalAsyncService;

    @Autowired
    private FinancialWithdrawalYsService financialWithdrawalYsService;

    @Autowired
    private FinancialSplitAsyncService financialSplitAsyncService;

    @Autowired
    private FinancialSplitYsService financialSplitYsService;

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Override
    public ApiResult ysWeChatPayReceipt(String requestParam) {
        FinancialPayAsyncDO financialPayAsyncDO = new FinancialPayAsyncDO();
        logger.info("FinancialYsCallBackServiceImpl.ysWeChatPayReceipt()-----获取到的银盛微信支付回执信息为:" + requestParam);
        /*获取mq消息*/
        Map<String, Object> map = JsonUtils.jsonToMap(requestParam);
        Object weChatPayReceipt = map.get("out_trade_no");
        String outTradeNo = weChatPayReceipt == null ? null : weChatPayReceipt.toString();
        weChatPayReceipt = map.get("total_amount");
        String totalAmount = weChatPayReceipt == null ? null : weChatPayReceipt.toString();
        /*初始化返回值值*/
        String result = FinancialSplitAsyncEnumConstants.ENUM_SEND_FAIL;
        try {
            financialPayAsyncDO.setGmtCreate(new Date());
            financialPayAsyncDO.setGmtModified(new Date());
            financialPayAsyncDO.setBusinessChannel(FinancialPayAsyncEnumConstants.ENUM_BUSINESSCHANNEL_YS);
            financialPayAsyncDO.setTotalAmount(new BigDecimal(totalAmount));
            financialPayAsyncDO.setOutTradeNo(outTradeNo);
            financialPayAsyncDO.setState(FinancialPayAsyncEnumConstants.ENUM_STATE_SUC);
            financialPayAsyncDO.setReturnMessage(requestParam);
            int res = financialPayAsyncService.save(financialPayAsyncDO);
            logger.info("FinancialYsCallBackServiceImpl.ysWeChatPayReceipt()-----银盛微信支付异步回执落库成功!");
            if (res > 0) {
                /*获取请求回调地址*/
                Map<String, Object> condMap = new HashMap<>();
                condMap.put("outTradeNo", outTradeNo);
                condMap.put("state", FinancialPayAsyncEnumConstants.ENUM_STATE_SUC);
                String resqContent = financialPayYsService.query(condMap);
                logger.info("FinancialYsCallBackServiceImpl.ysWeChatPayReceipt()-----银盛微信支付异步回执查询该笔订单的请求内容为!" + resqContent);
                if (resqContent != null && resqContent != "") {
                    /*查询resq_content内容中的notify_url回调地址*/
                    Map<String, Object> resqContentMap = JsonUtils.jsonToMap(resqContent);
                    String notifyUrl = (String) resqContentMap.get("notify_url");
                    logger.info("FinancialYsCallBackServiceImpl.ysWeChatPayReceipt()-----银盛微信支付异步回执查询该笔订单的回执地址!" + notifyUrl);
                    /*通知调用中心*/
                    result = httpClientUtils.doPostJson(notifyUrl, requestParam);
                    return ApiResult.success(result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYsCallBackServiceImpl.ysWeChatPayReceipt()-----银盛微信支付异步回执异常:" + e.getMessage());

        }
        return ApiResult.failure(result);
    }

    /**
     * 银盛支付宝支付异步回执
     *
     * @param requestParam
     * @return
     */
    @Override
    public ApiResult ysAliPayReceipt(String requestParam) {
        FinancialPayAsyncDO financialPayAsyncDO = new FinancialPayAsyncDO();
        logger.info("FinancialYsCallBackServiceImpl.ysAliPayReceipt()-----获取到的银盛支付宝支付回执信息为:" + requestParam);
        /*获取mq消息*/
        Map<String, Object> map = JsonUtils.jsonToMap(requestParam);
        Object aliPayReceipt = map.get("out_trade_no");
        String outTradeNo = aliPayReceipt == null ? null : aliPayReceipt.toString();
        aliPayReceipt = map.get("total_amount");
        String totalAmount = aliPayReceipt == null ? null : aliPayReceipt.toString();
        /*初始化返回值值*/
        String result = FinancialSplitAsyncEnumConstants.ENUM_SEND_FAIL;
        try {
            financialPayAsyncDO.setGmtCreate(new Date());
            financialPayAsyncDO.setGmtModified(new Date());
            financialPayAsyncDO.setBusinessChannel(FinancialPayAsyncEnumConstants.ENUM_BUSINESSCHANNEL_YS);
            financialPayAsyncDO.setTotalAmount(new BigDecimal(totalAmount));
            financialPayAsyncDO.setOutTradeNo(outTradeNo);
            financialPayAsyncDO.setState(FinancialPayAsyncEnumConstants.ENUM_STATE_SUC);
            financialPayAsyncDO.setReturnMessage(requestParam);
            int res = financialPayAsyncService.save(financialPayAsyncDO);
            logger.info("FinancialYsCallBackServiceImpl.ysAliPayReceipt()-----银盛支付宝支付异步回执落库成功!");
            if (res > 0) {
                /*获取请求回调地址*/
                Map<String, Object> condMap = new HashMap<>();
                condMap.put("outTradeNo", outTradeNo);
                condMap.put("state", FinancialPayYsEnumConstants.ENUM_STATE_SUC);
                String resqContent = financialPayYsService.query(condMap);
                logger.info("FinancialYsCallBackServiceImpl.ysAliPayReceipt()-----银盛支付宝支付异步回执查询该笔订单的请求内容为!" + resqContent);
                if (resqContent != null && resqContent != "") {
                    /*查询resq_content内容中的notify_url回调地址*/
                    Map<String, Object> resqContentMap = JsonUtils.jsonToMap(resqContent);
                    String notifyUrl = (String) resqContentMap.get("notify_url");
                    logger.info("FinancialYsCallBackServiceImpl.ysAliPayReceipt()-----银盛支付宝支付异步回执查询该笔订单的回执地址!" + notifyUrl);
                    /*通知调用中心*/
                    result = httpClientUtils.doPostJson(notifyUrl, requestParam);
                    return ApiResult.success(result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYsCallBackServiceImpl.ysWeChatPayReceipt()-----银盛支付宝支付异步回执异常:" + e.getMessage());

        }
        return ApiResult.failure(result);
    }

    /**
     * 银盛分账异步回执
     *
     * @param requestParam
     * @return
     */
    @Override
    public ApiResult ysSplitReceipt(String requestParam) {
        FinancialSplitAsyncDO financialSplitAsyncDO = new FinancialSplitAsyncDO();
        logger.info("FinancialYsCallBackServiceImpl.ysSplitReceipt()-----获取到的银盛分账回执信息为:" + requestParam);
        Map<String, Object> map = JsonUtils.jsonToMap(requestParam);
        Object ysSplitReceipt = map.get("out_trade_no");
        String outTradeNo = ysSplitReceipt == null ? null : ysSplitReceipt.toString();
        /*初始化返回值值*/
        String result = FinancialSplitAsyncEnumConstants.ENUM_SEND_FAIL;
        try {
            financialSplitAsyncDO.setGmtCreate(new Date());
            financialSplitAsyncDO.setGmtModified(new Date());
            financialSplitAsyncDO.setBusinessChannel(FinancialSplitAsyncEnumConstants.ENUM_BUSINESSCHANNEL_YS);
            financialSplitAsyncDO.setIsDelete(FinancialSplitAsyncEnumConstants.ENUM_ISDELETE_UNDEL);
            financialSplitAsyncDO.setOutTradeNo(outTradeNo);
            financialSplitAsyncDO.setState(FinancialSplitAsyncEnumConstants.ENUM_STATE_SUC);
            financialSplitAsyncDO.setReturnMessage(requestParam);
            int res = financialSplitAsyncService.save(financialSplitAsyncDO);
            logger.info("FinancialYsCallBackServiceImpl.ysSplitReceipt()-----银盛分账异步回执落库成功!");
            if (res > 0) {
                /*获取请求回调地址*/
                Map<String, Object> condMap = new HashMap<>();
                condMap.put("outTradeNo", outTradeNo);
                condMap.put("state", FinancialSplitYsEnumConstants.ENUM_STATE_SUC);
                String resqContent = financialSplitYsService.query(condMap);
                logger.info("FinancialYsCallBackServiceImpl.ysSplitReceipt()-----银盛分账异步回执查询该笔订单的请求内容为!" + resqContent);
                if (resqContent != null && resqContent != "") {
                    /*查询resq_content内容中的notify_url回调地址*/
                    Map<String, Object> resqContentMap = JsonUtils.jsonToMap(resqContent);
                    String notifyUrl = (String) resqContentMap.get("notify_url");
                    logger.info("FinancialYsCallBackServiceImpl.ysSplitReceipt()-----银盛分账异步回执查询该笔订单的回执地址!" + notifyUrl);
                    /*通知调用中心*/
                    result = httpClientUtils.doPostJson(notifyUrl, requestParam);
                    return ApiResult.success(result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYsCallBackServiceImpl.ysSplitReceipt()-----银盛分账异步回执异常:" + e.getMessage());

        }
        return ApiResult.failure(result);
    }

    /**
     * 银盛提现异步回执
     *
     * @param requestParam
     * @return
     */
    @Override
    public ApiResult ysWithdrawalReceipt(String requestParam) {
        FinancialWithdrawalAsyncDO financialWithdrawalAsyncDO = new FinancialWithdrawalAsyncDO();
        logger.info("FinancialYsCallBackServiceImpl.ysWithdrawalReceipt()-----获取到的银盛提现回执信息为:" + requestParam);
        Map<String, Object> map = JsonUtils.jsonToMap(requestParam);
        Object ysWithdrawalReceipt = map.get("out_trade_no");
        String outTradeNo = ysWithdrawalReceipt == null ? null : ysWithdrawalReceipt.toString();
        ysWithdrawalReceipt = map.get("total_amount");
        String totalAmount = ysWithdrawalReceipt == null ? null : ysWithdrawalReceipt.toString();
        /*初始化返回值值*/
        String result = FinancialWithdrawalAsyncEnumConstants.ENUM_SEND_FAIL;
        try {
            financialWithdrawalAsyncDO.setGmtCreate(new Date());
            financialWithdrawalAsyncDO.setGmtModified(new Date());
            financialWithdrawalAsyncDO.setBusinessChannel(FinancialWithdrawalAsyncEnumConstants.ENUM_BUSINESSCHANNEL_YS);
            financialWithdrawalAsyncDO.setTotalAmount(new BigDecimal(totalAmount));
            financialWithdrawalAsyncDO.setIsDelete(FinancialWithdrawalAsyncEnumConstants.ENUM_ISDELETE_UNDEL);
            financialWithdrawalAsyncDO.setOutTradeNo(outTradeNo);
            financialWithdrawalAsyncDO.setState(FinancialWithdrawalAsyncEnumConstants.ENUM_STATE_SUC);
            financialWithdrawalAsyncDO.setReturnMessage(requestParam);
            int res = financialWithdrawalAsyncService.save(financialWithdrawalAsyncDO);
            logger.info("FinancialYsCallBackServiceImpl.ysWithdrawalReceipt()-----银盛提现异步回执落库成功!");
            if (res > 0) {
                /*获取请求回调地址*/
                Map<String, Object> condMap = new HashMap<>();
                condMap.put("outTradeNo", outTradeNo);
                condMap.put("state", FinancialWithdrawalYsEnumConstants.ENUM_STATE_SUC);
                String resqContent = financialWithdrawalYsService.query(condMap);
                logger.info("FinancialYsCallBackServiceImpl.ysWithdrawalReceipt()-----银盛提现异步回执查询该笔订单的请求内容为!" + resqContent);
                if (resqContent != null && resqContent != "") {
                    /*查询resq_content内容中的notify_url回调地址*/
                    Map<String, Object> resqContentMap = JsonUtils.jsonToMap(resqContent);
                    String notifyUrl = (String) resqContentMap.get("notify_url");
                    logger.info("FinancialYsCallBackServiceImpl.ysWithdrawalReceipt()-----银盛提现异步回执查询该笔订单的回执地址!" + notifyUrl);
                    /*通知调用中心*/
                    result = httpClientUtils.doPostJson(notifyUrl, requestParam);
                    return ApiResult.success(result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYsCallBackServiceImpl.ysWithdrawalReceipt()-----银盛提现异步回执异常:" + e.getMessage());

        }
        return ApiResult.failure(result);
    }

    /**
     * 银盛代付异步回执
     *
     * @param requestParam
     * @return
     */
    @Override
    public ApiResult ysPaymentReceipt(String requestParam) {
        FinancialPaymentAsyncDO financialPaymentAsyncDO = new FinancialPaymentAsyncDO();
        logger.info("FinancialYsCallBackServiceImpl.ysPaymentReceipt()-----获取到的银盛代付回执信息为:" + requestParam);
        Map<String, Object> map = JsonUtils.jsonToMap(requestParam);
        Object ysPaymentReceipt = map.get("out_trade_no");
        String outTradeNo = ysPaymentReceipt == null ? null : ysPaymentReceipt.toString();
        ysPaymentReceipt = map.get("total_amount");
        String totalAmount = ysPaymentReceipt == null ? null : ysPaymentReceipt.toString();
        /*初始化返回值值*/
        String result = FinancialPaymentAsyncEnumConstants.ENUM_SEND_FAIL;
        try {
            financialPaymentAsyncDO.setGmtCreate(new Date());
            financialPaymentAsyncDO.setGmtModified(new Date());
            financialPaymentAsyncDO.setIsDelete(FinancialPaymentAsyncEnumConstants.ENUM_ISDELETE_UNDEL);
            financialPaymentAsyncDO.setBusinessChannel(FinancialPaymentAsyncEnumConstants.ENUM_BUSINESSCHANNEL_YS);
            financialPaymentAsyncDO.setTotalAmount(new BigDecimal(totalAmount));
            financialPaymentAsyncDO.setOutTradeNo(outTradeNo);
            financialPaymentAsyncDO.setState(FinancialPaymentAsyncEnumConstants.ENUM_STATE_SUC);
            financialPaymentAsyncDO.setReturnMessage(requestParam);
            int res = financialPaymentAsyncService.save(financialPaymentAsyncDO);
            logger.info("FinancialYsCallBackServiceImpl.ysPaymentReceipt()-----银盛代付异步回执落库成功!");
            if (res > 0) {
                /*获取请求回调地址*/
                Map<String, Object> condMap = new HashMap<>();
                condMap.put("outTradeNo", outTradeNo);
                condMap.put("state", FinancialPaymentYsEnumConstants.ENUM_STATE_SUC);
                String resqContent = financialPaymentYsService.query(condMap);
                logger.info("FinancialYsCallBackServiceImpl.ysPaymentReceipt()-----银盛代付异步回执查询该笔订单的请求内容为!" + resqContent);
                if (resqContent != null && resqContent != "") {
                    /*查询resq_content内容中的notify_url回调地址*/
                    Map<String, Object> resqContentMap = JsonUtils.jsonToMap(resqContent);
                    String notifyUrl = (String) resqContentMap.get("notify_url");
                    logger.info("FinancialYsCallBackServiceImpl.ysPaymentReceipt()-----银盛代付异步回执查询该笔订单的回执地址!" + notifyUrl);
                    /*通知调用中心*/
                    result = httpClientUtils.doPostJson(notifyUrl, requestParam);
                    return ApiResult.success(result);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYsCallBackServiceImpl.ysWithdrawalReceipt()-----银盛提现异步回执异常:" + e.getMessage());
        }
        return ApiResult.failure(result);
    }
}
